
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       srvrly_events.h
  * @author     baiyang
  * @date       2023-2-28
  ******************************************************************************
  */

#pragma once

#ifdef __cplusplus
extern "C"{
#endif

/*----------------------------------include-----------------------------------*/
#include <stdint.h>
#include <stdbool.h>

#include <relay/relay_config.h>
/*-----------------------------------macro------------------------------------*/

/*----------------------------------typedef-----------------------------------*/
// event control state
enum srvrly_event_type { 
    SRVRLY_EVENT_TYPE_RELAY=0,
    SRVRLY_EVENT_TYPE_SERVO=1
};

typedef struct servo_relay_events* servo_relay_events_t;
typedef const struct servo_relay_events* servo_relay_events_const_t;

struct servo_relay_events {
    enum srvrly_event_type type;

    // when the event was started in ms
    uint32_t start_time_ms;

    // how long to delay the next firing of event in millis
    uint16_t delay_ms;

    // how many times to cycle : -1 (or -2) = forever, 2 = do one cycle, 4 = do two cycles
    int16_t repeat;

    // RC channel for servos, relay number for relays
    uint8_t channel;

    // PWM for servos
    uint16_t servo_value;
};
/*----------------------------------variable----------------------------------*/

/*-------------------------------------os-------------------------------------*/

/*----------------------------------function----------------------------------*/
bool srvrly_do_set_servo(uint8_t channel, uint16_t pwm);
#if MB_RELAY_ENABLED
bool srvrly_do_set_relay(uint8_t relay_num, uint8_t state);
#endif
bool srvrly_do_repeat_servo(uint8_t _channel, uint16_t _servo_value, int16_t _repeat, uint16_t _delay_ms);
#if MB_RELAY_ENABLED
bool srvrly_do_repeat_relay(uint8_t relay_num, int16_t _repeat, uint32_t _delay_ms);
#endif
void srvrly_update_events(void);
/*------------------------------------test------------------------------------*/

#ifdef __cplusplus
}
#endif



